# -*- coding: utf-8 -*-
"""
@version: 1.0
@author: Pyma
@file: result
@time: 2019/10/16 20:00
"""

from django.http import JsonResponse

class HttpCode(object):
    ok = 200
    # 客户端请求的语法错误，服务器无法理解
    paramserror = 400
    # 请求要求用户的身份认证错误
    unauth = 401
    # 客户端请求中的方法被禁止
    methoderror = 405
    # 服务器内部错误
    servererror = 500

# {"code":400,"message":"","data":{}}
def result(code=HttpCode.ok, message="", data=None, kwargs=None):
    json_dict = {"code":code, "message":message, "data":data}
    # kwargs.keys() 字典里有值
    if kwargs and isinstance(kwargs, dict) and kwargs.keys():
        json_dict.update(kwargs)
    return JsonResponse(json_dict)

def ok():
    return result()

def params_error(message="", data=None):
    return result(code=HttpCode.paramserror, message=message, data=data)

def unauth(message="", data=None):
    return result(code=HttpCode.unauth, message=message, data=data)

def method_error(message="", data=None):
    return result(code=HttpCode.methoderror, message=message, data=data)

def server_error(message="", data=None):
    return result(code=HttpCode.servererror, message=message, data=data)




